package com.example.myapplication.module1.dialog

import android.app.Activity
import android.app.AlertDialog
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.myapplication.R
import com.example.myapplication.entity.AreaListBean
import com.example.myapplication.entity.DataDictBean
import com.example.myapplication.module1.adapter.AreaChooseAdapter
import com.example.myapplication.module1.adapter.ChooseTypeAdapter
import com.example.myapplication.module1.adapter.DataDictAdapter
import com.example.mylibrary.adapter.BaseRecyclerAdapter
import com.example.mylibrary.utils.UiTools

class ChooseTypeDialog(val activity: Activity) {
    private val dialog: AlertDialog = AlertDialog.Builder(activity).create()

    private val chooseTypeAdapter: ChooseTypeAdapter by lazy {
        ChooseTypeAdapter(activity)
    }

    private val dataDictAdapter: DataDictAdapter by lazy {
        DataDictAdapter(activity)
    }

    private val areaChooseAdapter: AreaChooseAdapter by lazy {
        AreaChooseAdapter(activity)
    }

    /**
     * 打开客户类型选择弹窗(纯文本弹窗)
     */
    fun getDialog(typeList: MutableList<String>) {
        val view = View.inflate(activity, R.layout.choose_type_dialog, null)
        val rvChooseList = view.findViewById<RecyclerView>(R.id.rvChooseList)
        rvChooseList.adapter = chooseTypeAdapter
        chooseTypeAdapter.typeList = typeList
        chooseTypeAdapter.clickListener = object : BaseRecyclerAdapter.ClickListener {
            override fun clickListener(position: Int, view: View?) {
                val typeList1 = chooseTypeAdapter.typeList
                if (typeList1.isNotEmpty()) {
                    confirmListener?.onConfirmListener(position, typeList1[position])
                    if (dialog.isShowing) {
                        dialog.dismiss()
                    }
                }
            }

            override fun longClickListener(position: Int, view: View?): Boolean {
                return false
            }
        }

        dialog.setCancelable(true)
        dialog.window?.setBackgroundDrawableResource(R.drawable.shape_white_bg10)
        dialog.show()
        val params = ViewGroup.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.MATCH_PARENT
        )
        dialog.setContentView(view, params)
        dialog.window?.setGravity(Gravity.CENTER)
//        dialog.window?.setWindowAnimations(R.style.middleEnterExit)
        dialog.window?.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
        val attributes = dialog.window?.attributes
        // dialog显示时背景的昏暗程度
        attributes?.dimAmount = 0.5f
        attributes?.width = UiTools.getDeviceWidth(activity) * 3 / 4
        attributes?.height =LinearLayout.LayoutParams.WRAP_CONTENT
        dialog.window?.attributes = attributes
    }

    interface ConfirmListener {
        fun onConfirmListener(position: Int, type: String)
    }

    var confirmListener: ConfirmListener? = null

    /**
     * 打开客户等级选择弹窗
     */
    fun getDataDialog(dataDictBeanList: MutableList<DataDictBean>?) {
        val view = View.inflate(activity, R.layout.choose_type_dialog, null)
        val rvChooseList = view.findViewById<RecyclerView>(R.id.rvChooseList)
        rvChooseList.adapter = dataDictAdapter
        dataDictBeanList?.let { dataDictAdapter.dataDictBeanList = it }
        dataDictAdapter.clickListener = object : BaseRecyclerAdapter.ClickListener {
            override fun clickListener(position: Int, view: View?) {
                val dataDictBeanList1 = dataDictAdapter.dataDictBeanList
                if (dataDictBeanList1.isNotEmpty()) {
                    dataDictChooseListener?.onConfirmListener(dataDictBeanList1[position])
                    if (dialog.isShowing) {
                        dialog.dismiss()
                    }
                }
            }

            override fun longClickListener(position: Int, view: View?): Boolean {
                return false
            }
        }

        dialog.setCancelable(true)
        dialog.window?.setBackgroundDrawableResource(R.drawable.shape_white_bg10)
        dialog.show()
        val params = ViewGroup.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT
        )
        dialog.setContentView(view, params)
        dialog.window?.setGravity(Gravity.CENTER)
//        dialog.window?.setWindowAnimations(R.style.middleEnterExit)
        dialog.window?.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
        val attributes = dialog.window?.attributes
        // dialog显示时背景的昏暗程度
        attributes?.dimAmount = 0.5f
        attributes?.width = UiTools.getDeviceWidth(activity) * 3 / 4
        attributes?.height =LinearLayout.LayoutParams.WRAP_CONTENT
        dialog.window?.attributes = attributes
    }

    interface DataDictChooseListener {
        fun onConfirmListener(dataDictBean: DataDictBean)
    }

    var dataDictChooseListener: DataDictChooseListener? = null

    /**
     * 打开区域选择弹窗
     */
    fun getAreaTypeDialog(dataDictBeanList: MutableList<DataDictBean>?) {
        val view = View.inflate(activity, R.layout.choose_type_dialog, null)
        val rvChooseList = view.findViewById<RecyclerView>(R.id.rvChooseList)
        rvChooseList.adapter = dataDictAdapter
        dataDictBeanList?.let { dataDictAdapter.dataDictBeanList = it }
        dataDictAdapter.clickListener = object : BaseRecyclerAdapter.ClickListener {
            override fun clickListener(position: Int, view: View?) {
                val dataDictBeanList1 = dataDictAdapter.dataDictBeanList
                if (dataDictBeanList1.isNotEmpty()) {
                    areaTypeChooseListener?.onConfirmListener(dataDictBeanList1[position])
                    if (dialog.isShowing) {
                        dialog.dismiss()
                    }
                }
            }

            override fun longClickListener(position: Int, view: View?): Boolean {
                return false
            }
        }

        dialog.setCancelable(true)
        dialog.window?.setBackgroundDrawableResource(R.drawable.shape_white_bg10)
        dialog.show()
        val params = ViewGroup.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.MATCH_PARENT
        )
        dialog.setContentView(view, params)
        dialog.window?.setGravity(Gravity.CENTER)
//        dialog.window?.setWindowAnimations(R.style.middleEnterExit)
        dialog.window?.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
        val attributes = dialog.window?.attributes
        // dialog显示时背景的昏暗程度
        attributes?.dimAmount = 0.5f
        attributes?.width = UiTools.getDeviceWidth(activity) * 3 / 4
        attributes?.height =LinearLayout.LayoutParams.WRAP_CONTENT
        dialog.window?.attributes = attributes
    }

    interface AreaTypeChooseListener {
        fun onConfirmListener(dataDictBean: DataDictBean)
    }

    var areaTypeChooseListener: AreaTypeChooseListener? = null

    /**
     * 打开区域选择弹窗
     */
    fun getAreaListDialog(areaListBean: MutableList<AreaListBean>?, level: String) {
        val view = View.inflate(activity, R.layout.choose_area_dialog, null)
        val rvChooseList = view.findViewById<RecyclerView>(R.id.rvChooseList)
        val tv25 = view.findViewById<TextView>(R.id.tv25)
        when (level) {
            "1" -> {
                // 国家
                tv25.text = UiTools.getString(R.string.chooseCountry)
            }
            "2" -> {
                // 省
                tv25.text = UiTools.getString(R.string.chooseProvince)
            }
            "3" -> {
                // 市
                tv25.text = UiTools.getString(R.string.chooseCity)
            }
            "4" -> {
                // 区
                tv25.text = UiTools.getString(R.string.chooseArea)
            }
        }

        rvChooseList.adapter = areaChooseAdapter
        areaListBean?.let { areaChooseAdapter.areaList = it }
        areaChooseAdapter.clickListener = object : BaseRecyclerAdapter.ClickListener {
            override fun clickListener(position: Int, view: View?) {
                val areaList = areaChooseAdapter.areaList
                if (areaList.isNotEmpty()) {
                    areaListChooseListener?.onConfirmListener(areaList[position], level)
                    if (dialog.isShowing) {
                        dialog.dismiss()
                    }
                }
            }

            override fun longClickListener(position: Int, view: View?): Boolean {
                return false
            }
        }

        dialog.setCancelable(false)
        dialog.window?.setBackgroundDrawableResource(R.drawable.shape_white_bg10)
        dialog.show()
        val params = ViewGroup.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.MATCH_PARENT
        )
        dialog.setContentView(view, params)
        dialog.window?.setGravity(Gravity.CENTER)
//        dialog.window?.setWindowAnimations(R.style.middleEnterExit)
        dialog.window?.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
        val attributes = dialog.window?.attributes
        // dialog显示时背景的昏暗程度
        attributes?.dimAmount = 0.5f
        attributes?.width = UiTools.getDeviceWidth(activity) * 3 / 4
        attributes?.height = UiTools.getDeviceHeight(activity) * 3 / 4
        dialog.window?.attributes = attributes
    }

    interface AreaListChooseListener {
        fun onConfirmListener(areaList: AreaListBean, level: String)
    }

    var areaListChooseListener: AreaListChooseListener? = null
}